package com.fanhao.dllo.justforyou.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.entity.NewsRecyclerResult;
import com.fanhao.dllo.justforyou.volley.GetVolleyDatasUtil;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class NewsRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NewsRecyclerResult.T1348647909107Entity> t1348647909107;
    private NewsRecyclerResult.T1348647909107Entity t1348647909107Entity;

    private LayoutInflater inflater;
    private Context context;


    private OnNewsRecyItemClickListener listener;

    // 跳转事件 的抽象接口
    public interface OnNewsRecyItemClickListener {
        void recyItemClickListener(NewsRecyclerResult.T1348647909107Entity nr);
    }


    public void setListener(OnNewsRecyItemClickListener listener) {
        this.listener = listener;
    }

    public NewsRecyclerAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<NewsRecyclerResult.T1348647909107Entity> datas) {
        t1348647909107 = datas;
        notifyDataSetChanged();

    }

    private static final int TYPE00 = 0;
    private static final int TYPE01 = 1;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {



        inflater = LayoutInflater.from(context);
        switch (viewType) {
            case TYPE00:

                // 根据数据中中否有imgextra来判断添加哪个界面
                View view = inflater.inflate(R.layout.fragment_news__item_recycler1, null);


                return new RecyclerViewHolder00(view);
            case TYPE01:
                View view1 = inflater.inflate(R.layout.fragment_news_item_recycler2, null);


                return new RecyclerViewHolder01(view1);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (t1348647909107 != null) {
            NewsRecyclerResult.T1348647909107Entity t1348647909107Entity =
                    t1348647909107.get(position);

            if (holder instanceof RecyclerViewHolder00) {
                RecyclerViewHolder00 holder00 = (RecyclerViewHolder00) holder;
                holder00.titleTv.setText(t1348647909107Entity.getTitle());
                holder00.digestTv.setText(t1348647909107Entity.getDigest());

                String url = t1348647909107Entity.getImgsrc();
                GetVolleyDatasUtil.getBitmapRequest(url, holder00.imgIv);
                holder00.itemposition = position;
            } else {
                List<NewsRecyclerResult.T1348647909107Entity.ImgextraEntity> imgextra =
                        t1348647909107Entity.getImgextra();
                RecyclerViewHolder01 holder01 = (RecyclerViewHolder01) holder;
                holder01.titleTv.setText(t1348647909107Entity.getTitle());
                holder01.priorityTv.setText(t1348647909107Entity.getPriority() + "");
                holder01.itempositions = position;
                if (imgextra != null && imgextra.size() > 0) {
                    String url01 = t1348647909107Entity.getImgsrc();
                    String url02 = imgextra.get(0).getImgsrc();
                    String url03 = imgextra.get(1).getImgsrc();

                    GetVolleyDatasUtil.getBitmapRequest(url01, holder01.imgIv01);
                    GetVolleyDatasUtil.getBitmapRequest(url02, holder01.imgIv02);
                    GetVolleyDatasUtil.getBitmapRequest(url03, holder01.imgIv03);
                }
            }
        }

    }


    @Override
    public int getItemCount() {
        return t1348647909107 != null && t1348647909107.size() > 0 ? t1348647909107.size() : 0;
    }

    /**
     * 判断根据什么特性进行添加哪个界面
     **/
    @Override
    public int getItemViewType(int position) {
        NewsRecyclerResult.T1348647909107Entity t1348647909107Entity =
                t1348647909107.get(position);

        if (t1348647909107Entity.getImgextra() != null && t1348647909107Entity.getImgextra().size() > 0) {
            return TYPE01;
        } else
            return TYPE00;
    }


    /**
     * 初始化RecyclerViewHolder00的组件
     ***/
    public class RecyclerViewHolder00 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView titleTv, digestTv;
        private ImageView imgIv;
        private LinearLayout linearLayout;
        private int itemposition;


        public RecyclerViewHolder00(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            digestTv = (TextView) itemView.findViewById(R.id.digestTv);

            imgIv = (ImageView) itemView.findViewById(R.id.imageIv);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);

            linearLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (t1348647909107 != null) {

                NewsRecyclerResult.T1348647909107Entity nr = t1348647909107.get(itemposition);
                if (listener != null) {
                    listener.recyItemClickListener(nr);
                }
            }
        }
    }

    /**
     * 初始化RecyclerViewHolder01的组件
     ***/
    public class RecyclerViewHolder01 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView titleTv;
        private TextView priorityTv;
        private ImageView imgIv01, imgIv02, imgIv03;
        private LinearLayout linearLayout;
        private int itempositions;

        public RecyclerViewHolder01(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.setnameTv);
            priorityTv = (TextView) itemView.findViewById(R.id.priorityTv);

            imgIv01 = (ImageView) itemView.findViewById(R.id.imgIv01);
            imgIv02 = (ImageView) itemView.findViewById(R.id.imgIv02);
            imgIv03 = (ImageView) itemView.findViewById(R.id.imgIv03);

            linearLayout = (LinearLayout)itemView.findViewById(R.id.linearLayout);
            linearLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (t1348647909107 != null) {

                NewsRecyclerResult.T1348647909107Entity nr = t1348647909107.get(itempositions);
                if (listener != null) {
                    listener.recyItemClickListener(nr);
                }
            }
        }
    }


}




